<?php

/*
 * This class is used for as Model for Agent table 
 */

class Model_Agent extends Model_Base {

    protected $_name = 'tbl_agent';
    protected $_primaryKey = 'ag_id';

    public function getStatusArray() {
        return array(
            '' => '-- Select Status --',
            '1' => 'Active',
            '0' => 'Inactive'
        );
    }

    /**
     * Fetch an individual entry
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryByViewId($id) {

        $select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('ag' => 'tbl_agent'))
                ->where('ag_id = ?', $id)
                ->where('ag.ag_status != 2');
        #echo $select;exit;
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('ag' => 'tbl_agent'))
                ->setIntegrityCheck(false)
                ->where('ag_id = ?', $id)
                ->where('ag_status != 2');

        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get agents
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchAgent($where = "", $limit = "", $offset = "0", $order = 'ag_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('adm' => 'tbl_agent'))
                ->setIntegrityCheck(false)
                ->where('ag_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getAgentCount($where) {
        $select = $this->select()
                ->from(array('adm' => 'tbl_agent'), Array('cnt' => 'count(*)'))
                ->where('ag_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }

    /**
     *  This function is used to delete agents
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function deleteAgent($prdtArray) {
        $loginModel = new Model_Login();
        if (is_array($prdtArray)) {
            foreach ($prdtArray as $_agent) {
                $agentData = Array();
                $agentData = $this->fetchEntryById($_agent);
                if (count($agentData) > 0) {
                    $data = array('ag_status' => '2');
                    $this->update($data, 'ag_id =' . (int) $_agent);
                }
            }
        } else {
            $ag_id = $prdtArray;
            $agentData = $this->fetchEntryById($ag_id);
            if (count($agentData) > 0) {
                $data = array('ag_status' => '2');
                $this->update($data, 'ag_id =' . (int) $ag_id);
            }
        }
    }

}
